<%@page import="com.sample.library.service.BookLocalServiceUtil"%>
<%@ include file="/jsp/include.jsp" %>
<%
RowChecker rowChecker = null;
rowChecker = new RowChecker(renderResponse);

PortletURL portletURL = renderResponse.createRenderURL();
String actionStr = ParamUtil.getString(request, "action");
portletURL.setParameter("action", actionStr);
String portletURLString = portletURL.toString();

User _user = null;
if (themeDisplay != null) {
	_user = themeDisplay.getUser();
}

%>
<portlet:actionURL var="formAction">
	<portlet:param name="action" value="viewBook" />
</portlet:actionURL>
	<h3><liferay-ui:message key="Book.View.Title" /></h3>
		<form class="uni-form" action="<%= portletURLString %>" method="post" name="<portlet:namespace />fm" onSubmit="submitForm(this); return false;">
		<liferay-portlet:renderURLParams varImpl="portletURL" />
			<%
				request.setAttribute("toolbarItem", "view-all");
				pageContext.include("toolbar.jsp");
			%>
			<liferay-ui:search-container 
				rowChecker="<%= rowChecker %>"
				searchContainer="<%= new BookSearch(renderRequest, portletURL) %>"
			>
			<input name="<portlet:namespace />deleteRowIds" type="hidden" value="" />
			
			<liferay-ui:search-form page="//jsp/book/search.jsp"
				searchContainer="<%= searchContainer %>"
				servletContext="<%=this.getServletContext() %>"/>
			<%
				BookSearchTerms searchTerms = (BookSearchTerms)searchContainer.getSearchTerms();
				if(_user != null)	{
					searchTerms.setUserId(_user.getUserId());
				}
			%>
			<%--
				results="<%= BookLocalServiceUtil.search(searchTerms.getTitle(), searchTerms.getUserId(), searchContainer.getStart(), searchContainer.getEnd(), searchContainer.getOrderByComparator()) %>"
				total="<%= BookLocalServiceUtil.searchCount(searchTerms.getTitle(), searchTerms.getUserId()) %>"/>
			 --%>
			<liferay-ui:search-container-results 
				results="<%= BookLocalServiceUtil.search(searchContainer, searchContainer.getStart(), searchContainer.getEnd(), searchContainer.getOrderByType(), searchContainer.getOrderByComparator()) %>"
				total="<%= BookLocalServiceUtil.searchCount(searchContainer) %>"/>
			
			<liferay-ui:search-container-row
				className="com.sample.library.model.Book"
				escapedModel="<%= false %>"
				keyProperty="bookId"
				modelVar="book"
				>
				<portlet:renderURL windowState="<%= WindowState.MAXIMIZED.toString() %>" var="rowURL">
					<portlet:param name="action" value="editBook" />
					<portlet:param name="redirect" value="<%= searchContainer.getIteratorURL().toString() %>" />
					<portlet:param name="bookId" value="<%= String.valueOf(book.getBookId()) %>" />
				</portlet:renderURL>
				<liferay-ui:search-container-column-text
					href="<%= rowURL %>"
					name='<%= LanguageUtil.get(response.getLocale(), "Book.Title") %>'
					orderable="<%= true %>"
					property="title"
				/>
				<liferay-ui:search-container-column-text
					href="<%= rowURL %>"
					name='<%= LanguageUtil.get(response.getLocale(), "Book.UserName") %>'
					orderable="<%= true %>"
					property="userName"
				/>
	 			<liferay-ui:search-container-column-jsp
					align="right"
					path="//jsp/book/action.jsp"
				/>
				</liferay-ui:search-container-row>
			<liferay-ui:search-iterator />
			</liferay-ui:search-container>
			<div class="separator"><!-- --></div>
			<aui:button onClick='<%= renderResponse.getNamespace() + "deleteEntries();" %>' value="delete" />
			</form>
<aui:script>
	Liferay.provide(
		window,
		'<portlet:namespace />deleteEntries',
		function() {
			var selectedIds = Liferay.Util.listCheckedExcept(document.<portlet:namespace />fm, "<portlet:namespace />allRowIds");
			var arrayId = selectedIds.split(',');
			var yesId = new Boolean(0);

			if(arrayId != null && arrayId.length > 0)	{
				for(var i = 0; i < arrayId.length; i++)	{
					if(arrayId[i] != 'true' && arrayId[i] != 'on')	{
						yesId = new Boolean(1);
						break;
					}
				}
			}
			if (!selectedIds || yesId == false) {
				message = '<%=UnicodeLanguageUtil.get(pageContext, "no-rows-selected")%>';
				alert(message);
				return;
			}
			else	{
				if (confirm('<%= UnicodeLanguageUtil.get(pageContext, "are-you-sure-you-want-to-delete-the-selected-entries") %>')) {
					document.<portlet:namespace />fm.method = "post";
					document.<portlet:namespace />fm.<portlet:namespace />deleteRowIds.value = selectedIds;
					submitForm(document.<portlet:namespace />fm, "<portlet:actionURL><portlet:param name="action" value="viewBook" /></portlet:actionURL>");
				}
			}
		},
		['liferay-util-list-fields']
	);
</aui:script>
